package org.univie.mappr;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;

public class FullscreenView extends Activity implements OnGestureListener {

	// debugging
	private static final String TAG = "org.univie.mappr.FullscreenView";

	private ImageView mImageView;
	private Intent mIntent;
	private FlickrPhoto mPhoto;
	private GestureDetector gestureListener;
	private ProgressDialog mLoadingDialog;
	private int mdone = 0;
	private boolean mOnPause = false;

	private OnClickListener mClickListener = new OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			Context cont = mImageView.getContext();
			Intent intent = new Intent(cont, MetadataView.class);
			cont.startActivity(intent);
		}
	};

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		class DownloadTask extends AsyncTask<Void, String, Drawable> {

			@Override
			protected Drawable doInBackground(Void... params) {
				return FullscreenCache.getDrawable(mPhoto);
			}

			protected void onPostExecute(Drawable photo) {
				// dismissing the dialog
				mdone = 100;
				mLoadingDialog.dismiss();
				// making toast to whatever other activity is in foreground
				if (mOnPause) {
					try {
						Toast.makeText(mImageView.getContext(),
								"The Photo is loaded - swipe back, to see it",
								Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						Log.e(TAG,
								"error in making toast for mImageView context");
					}
				}
				// get the image view
				mImageView.setImageDrawable(photo);
				Log.d(TAG, "post Execute ... loading done");
			}
		}

		Log.d(TAG, "Started new Fullscreen activity");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.fullscreen_view);

		mImageView = (ImageView) findViewById(R.id.fullscreen_view_image_view);

		// gestureDetector

		gestureListener = new GestureDetector(this);

		// assign the photo to the one selected in the MapprView
		mPhoto = MapprView.sCurrentPhoto;

		// ProgressDialog
		mLoadingDialog = new ProgressDialog(mImageView.getContext());
		// mLoadingDialog.setTitle("Loading Photo");
		mLoadingDialog.setCancelable(false);
		mLoadingDialog.setIndeterminate(false);
		mLoadingDialog.setButton(ProgressDialog.BUTTON1, "Show Metadata",
				mClickListener);
		mLoadingDialog.setMessage("Loading Photo");
		mdone = 0;
		mLoadingDialog.show();

		DownloadTask download = new DownloadTask();
		download.execute();
	}

	@Override
	public void onResume() {
		super.onResume();
		// if the photo isn't loaded the dialog gets shown
		if (mdone != 100) {
			mLoadingDialog.show();
		}
		mOnPause = false;
	}

	@Override
	public void onPause() {
		super.onPause();
		// if dialog is shown the dialog gets dismissed
		// i quess this is made automatic but only to be sure
		if (mdone != 100) {
			mLoadingDialog.dismiss();
		}
		// telling the app we are onpause
		mOnPause = true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent me) {

		return gestureListener.onTouchEvent(me);

	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {

		if (distanceX < 0) {
			// scroll left
			Log.d(TAG, "scrolled left");
		}
		if (distanceX > 50) {
			// scroll right
			Log.d(TAG, "scrolled right");
			Context cont = mImageView.getContext();
			Log.d(TAG, "behind getContext");

			Intent intent = new Intent(cont, MetadataView.class);
			Log.d(TAG, "behind Intend");
			cont.startActivity(intent);
			Log.d(TAG, "behind startActivity");

			return true;
		}
		if (distanceY < 0) {
			// scroll up
			Log.d(TAG, "scrolled up");

		}
		if (distanceY > 0) {
			Log.d(TAG, "scrolled down");
			// scroll down
		}
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}
